package com.compile;

import java.util.Collections;
import java.util.List;

/**
 * Created by zhoulikai on 17/2/17.
 */

public class JavaMake implements IJavaMake {

    public static final String KEY_PREFIX = "KEY_";
    public static final String KEY_VALUE_SUFFIX = "_VALUE";
    public static final String KEY_REQUIRE_SUFFIX = "_REQUIRE";

    public static final String clsSpace = "    ";

    //依赖的包
    public final static String[] importPkg = {
            "import java.util.HashMap;",
            "import java.util.Map;",
            "import java.util.Collections;"
    };

    //依赖的所需的固定变量
    public final static String[] staticVariable = {
            "public static  Map<String, Map<String, Boolean>> clsMap = new HashMap<>();"
    };

    public final static String STATIC_CODE_BLOCK_PREFIX = "static {";

    @Override
    public String makeJavaFile(String fileName, String path) {
        if (fileName == null || path == null || "".equals(fileName) || "".equals(path)) {
            return null;
        }
        String names[] = fileName.split("\\.");
        if (names == null || names.length == 0) {
            return null;
        }
        //类名称
        String className = names[names.length - 1];
        String packName = fileName.substring(0, fileName.length() - className.length() - 1);
        for (int i = 0; i < names.length - 1; i++) {
            path += names[i] + "/";
        }

//        if (path.endsWith("/")) {
//            path = path.substring(0, path.length() - 1);
//        }

        System.out.print(">>>path>>>" + path + ">>>>>classname>>>>" + className);

        String javaContent = "";
        if (packName != null && !"".equals(packName)) {
            javaContent += "package " + packName + ";\r\n";
        }

        //添加依赖的包
        if (importPkg != null && importPkg.length != 0) {
            javaContent += "\r\n\r\n";
            for (int i = 0; i < importPkg.length; i++) {
                javaContent += importPkg[i] + "\r\n";
            }
        }

        //添加类标识
        javaContent += "public class " + className + " {\r\n";


        //添加类的固定的常量值
        if (staticVariable != null && staticVariable.length != 0) {
            javaContent += "\r\n";
            for (int i = 0; i < staticVariable.length; i++) {
                javaContent += clsSpace + staticVariable[i] + "\r\n";
            }

        }

        //添加不可修改静态区
        javaContent += "\r\n";
        javaContent += clsSpace + STATIC_CODE_BLOCK_PREFIX + "\r\n\r\n";
        javaContent += clsSpace + clsSpace + "clsMap = Collections.unmodifiableMap(clsMap);\r\n";
        javaContent += clsSpace + "};" + "\r\n";

        javaContent += "}";
        return Tools.createFile(className, javaContent, path);
    }

    @Override
    public void makeJavaClass(String javaFile, Class cls, List<Field> fields) {
        if (cls == null || fields == null || cls.name == null || "".equals(cls.name)) {
            return;
        }

        String clsContent = "\r\n";
        try {
            String clsSpace = "    ";

            //写类注释
            if (cls.note != null && !"".equals(cls.note)) {
                clsContent += clsSpace + "// " + cls.note + "\r\n";
            }

            clsContent += clsSpace + getJavaModified(cls.modification) + " class " + cls.name + " {\r\n";

            //静态代码块代码
            String staticContent = "\r\n\r\n";
            String mapName = cls.name + "_map";
            staticContent += clsSpace + clsSpace + "Map<String, Boolean> " + mapName + " = " + "new HashMap<>();\r\n";

            //成员变量
            String filedSpace = clsSpace + clsSpace;
            for (int i = 0; i < fields.size(); i++) {
                Field field = fields.get(i);
                if (field != null) {
                    if (field.name != null && !"".equals(field.name)) {
                        clsContent += "\r\n";
                        //写类变量注释
                        if (field.note != null && !"".equals(field.name)) {
                            clsContent += filedSpace + "// " + field.note + "\r\n";
                        }
                        //写入变量值
                        String keyValue = KEY_PREFIX + field.name + KEY_VALUE_SUFFIX;
                        String keyRequire = KEY_PREFIX + field.name + KEY_REQUIRE_SUFFIX;
                        clsContent += filedSpace + getJavaModified(field.modification) + " String " + keyValue.toUpperCase() + " = " + "\"" + keyValue.toLowerCase() + "\"" + ";\r\n";
                        if (field.value == null || "".equals(field.value)) {
                            field.value = "false";
                        }
                        //    clsContent += filedSpace + getJavaModified(field.modification) + " boolean " + keyRequire.toUpperCase() + " = " + field.value + ";\r\n";
                        staticContent += clsSpace + clsSpace + mapName + ".put(" + cls.name + "." + keyValue.toUpperCase() + ", " + field.value + ");\r\n";
                    }

                }
            }

            clsContent += "\r\n";
            clsContent += clsSpace + "}\r\n";

            Tools.writeFileContent(javaFile, clsContent, "}", Tools.INSERT_DIRECTION_BEFORE);

            //将静态代码写入
            staticContent += clsSpace + clsSpace + "clsMap.put(\"" + cls.name + "\", " + mapName + ");";
            Tools.writeFileContent(javaFile, staticContent, STATIC_CODE_BLOCK_PREFIX, Tools.INSERT_DIRECTION_AFTER);

        } catch (Exception e) {

        }

    }

    public String getJavaModified(int modification) {
        String modifiedFlag = "";
        if ((modification & Modification.PUBLIC) == Modification.PUBLIC) {
            modifiedFlag += "public  ";
        }
        if ((modification & Modification.STATIC) == Modification.STATIC) {
            modifiedFlag += "static ";
        }
        if ((modification & Modification.FINAL) == Modification.FINAL) {
            modifiedFlag += "final";
        }
        return modifiedFlag;
    }
}
